<?php

namespace app\index\controller;

use app\index\Controller;
use think\Db;
use think\Validate;


/**
 * @title 路线（模块04）
 * @description 接口说明
 * @group 路线
 * @header name:model_id require:0 default:04 desc:模块id
 * @groupremark 已使用错误代码1040001,1040002,1040003,1040004
 */
class Road extends Controller
{
    /**
     * @var array 白名单方法，如果设置会覆盖黑名单方法，只允许白名单方法能正常访问
     */
    protected static $allowList = ['getlist', 'detail', 'get_route', 'get_top_route','get_categorys'];
    /**
     * @title 分类列表
     * @description 接口说明
//     * @param name:page type:int require:1 other: desc:页码
     * @author 梁敏
     * @url /index/road/get_categorys
     * @method ~
     *
     *
     */
    public function get_categorys()
    {
        $data = input();
//        $listRows = $this->request->param('numPerPage') ?: Config("paginate.list_rows");
        $model= Db::table('tp_route_category');
        $list=$model->select();
        if (!empty($list)) {
            $msg['list'] = $list;
//            $msg['total'] = $list->total();
//            $msg['per_page'] = $list->listRows();
//            $msg['current_page'] = $list->currentPage();
            $msg['message'] = "获取成功";
            return $this->success($msg);
        } else {
            $msg['error_id'] = '1040002';
            $msg['message'] = '获取路线分类失败';
            return $this->error($msg);
        }

    }
    /**
     * @title 列表
     * @description 接口说明
     * @param name:page type:int require:1 other: desc:页码
     * @param name:category_id type:int require:1 other: desc:分类id
     * @author 庆锋
     * @url /index/road/getlist
     * @method ~
     *
     *
     */
    public function getlist()
    {
        $data = input();
        $listRows = $this->request->param('numPerPage') ?: Config("paginate.list_rows");
         $model= Db::table('tp_route');
        $model->alias('r')
            ->join('tp_upload_file f', 'r.main_img=f.id', 'left')
            ->field('r.*,CONCAT("' . get_host() . '",f.url) as url')
            ->where('status', 1)
            ->where('isdelete', 0)
            ->order('r.id', 'desc');
        if($data['category_id']){
            $model->where('category_id',$data['category_id']);
        }
            $list=$model->paginate($listRows, false, ['query' => $this->request->get(), "page" => input("page")]);
        //dump($list);
        $listData = $list->items();
        foreach ($listData as $index => $listDatum) {
            unset($listData[$index]['go_time']); // 销毁出发时间
            $listData[$index]['days'] = empty($listDatum['day']) ? 0 : $listDatum['day'];
        }
        if (!empty($list)) {
            $msg['list'] = $listData;
            $msg['total'] = $list->total();
            $msg['per_page'] = $list->listRows();
            $msg['current_page'] = $list->currentPage();
            $msg['message'] = "获取成功";
            return $this->success($msg);
        } else {
            $msg['error_id'] = '1040002';
            $msg['message'] = '获取路线失败';
            return $this->error($msg);
        }

    }

    /**
     * @title 详情
     * @description 接口说明
     * @param name:id type:int require:1 other: desc:路线id
     * @author 庆锋
     * @url /index/road/detail
     * @method ~
     *
     */
    public function detail()
    {

        $data = input();
        logs('road666', $data);
        $vmsg = [
            'id.require' => '缺少id',
        ];

        //检测是否传参
        $validate = new Validate([
            'id' => 'require',
        ], $vmsg);

        if (!$validate->check($data)) {
            $msg['error_id'] = '1040003';
            $msg['message'] = $validate->getError();
            return $this->error($msg);
        }
        $map['r.id'] = $data['id'];
        $route = Db::name('route')
            ->alias('r')
            ->where($map)
            ->join('tp_upload_file f', 'r.main_img=f.id', "left")
            ->field('r.*,CONCAT("' . get_host() . '",f.url) as url')
            ->find();
        if (!empty($route)) {

            $msg['data'] = $route;
            return $this->success($msg);
        } else {
            $msg['error_id'] = '1040004';
            $msg['message'] = '获取路线失败';
            return $this->error($msg);
        }

    }

    /**
     * @title 获取置顶路线
     * @description 接口说明
     * @author 李锐志
     * @url /index/road/get_top_route
     * @method ~
     */
    public function get_top_route()
    {
        $where = [
            'is_top' => 1,
            'isdelete' => 0
        ];

        $result = Db::name('route')
            ->where($where)
            ->field('id,title,tag,main_img,day,go_time,create_time,end_time')
            ->order('top_time', 'desc')
            ->select();

        foreach ($result as $k => $v) {
             $head_img = Db::name('upload_file')
                ->where('id',$v['main_img'])
                ->find();

            $result[$k]['main_img'] = $head_img['domain'] . $head_img['url'];
        }

        if ($result) {
            $msg['data'] = $result;
            $msg['message'] = '获取置顶线路成功';
            return $this->success($msg);
        } else {
            $msg['message'] = '获取置顶线路失败';
            return $this->error($msg);
        }
    }

    /**
     * @title 线路列表
     * @description 接口说明
     * @author 李锐志
     * @url /index/road/get_route
     * @method ~
     *
     */
    public function get_route()
    {
        $route = Db::name('route')
            ->where('isdelete', 0)
            ->field('id,title,main_img')
            ->select();

        $result = Db::name('route_category')
            ->where('isdelete', 0)
            ->field('id,name')
            ->select();

        array_unshift($result, ['name' => '全部']);



        foreach ($result as $k => $v) {
            $result[$k]['route'] = Db::name('route')
                ->where('category_id', $v['id'])
                ->where('isdelete', 0)
                ->field('id,title,main_img')
                ->select();

            foreach ($result[$k]['route'] as $k2 => $v2) {
                $main_img = Db::name('upload_file')
                    ->where('id', $v2['main_img'])
                    ->find();
                $result[$k]['route'][$k2]['main_img'] = $main_img['domain'] . $main_img['url'];
            }
        }

        $result[0]['route'] = $route;

        if ($result) {
            $msg['data'] = $result;
            $msg['message'] = '获取线路列表成功';
            return $this->success($msg);
        } else {
            $msg['message'] = '获取线路列表失败';
            return $this->error($msg);
        }
    }
}
